package org.lex.jdbc.url.view.internal;

import java.io.IOException;
import java.util.List;

import org.lex.jdbc.url.DriverConfig;
import org.lex.jdbc.url.persist.IDriverDao;
import org.lex.jdbc.url.view.IJdbcConnectionModel;
import org.lex.jdbc.url.view.internal.JdbcConnectionTreeNode.NodeType;

public class JdbcConnectionTreeNodeHelper {
	private static final String ROOT_NODE_TEXT = "Database";

	public JdbcConnectionTreeNode createRootNode(IJdbcConnectionModel model)
			throws IOException {
		JdbcConnectionTreeNode root = new JdbcConnectionTreeNode(NodeType.TEXT,
				false);
		root.setUserObject(ROOT_NODE_TEXT);
		IDriverDao driverDao = model.getDriverDao();
		Object[][] drivers = { { driverDao.getSystemDrivers(), false },
				{ driverDao.getUserDrivers(), true } };
		for (Object[] para : drivers) {
			boolean editable = (Boolean) para[1];
			for (DriverConfig driver : (List<DriverConfig>) para[0]) {
				JdbcConnectionTreeNode driverNode = this.createDriverNode(
						driver, editable);
				root.add(driverNode);
			}
		}
		return root;
	}

	public JdbcConnectionTreeNode createDriverNode(DriverConfig driver,
			boolean editable) {
		JdbcConnectionTreeNode node = new JdbcConnectionTreeNode(
				NodeType.DRIVER, editable);
		node.setUserObject(driver);
		return node;
	}
}
